<!DOCTYPE html>
<html>
<head>
<base href="#(basePath)" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>无标题文档</title>
<style type="text/css">
   h1 {font-size: 30px !important;}
</style>
<link rel="stylesheet" href="plug/layui/css/layui.css">
<script type="text/javascript" src="plug/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="plug/layui/lay/dest/layui.all.js"></script>
<script>
	$(function(){
		InitQuestions.list();
		InitQuestions.bindClickEvent();
	});
	var InitQuestions = {
		list : function list(){
			var requestSc = $.ajax({
				dataType : 'json',
				type : "POST",
				async:false,
				url : "question/list",
				data : {}
			});
			requestSc.done(function(result, tst, jqXHR) {
				if(result.isOk){
   					var html = '';
   					$(result.list).each(function(i,item){
   						var isShow = item.isShow == 0 ? 'checked' : '';
   						html +='<tr class="tr"><td><input type="checkbox" lay-skin="primary"></td><td>'+(i+1)+'</td>'
			            	+'<td>'+item.content+'</td>'
			            	+'<td>'+item.use_time+'</td>'
			            	+'<td><input type="checkbox" class="isshow" data-id="'+item.qid+'" data-show="'+item.isShow+'" lay-filter="isshow" lay-skin="switch" lay-text="显示|关闭" '+isShow+' ></td>'
			                +'<td></td>'
			                +'<td><span data-id="'+item.qid+'" data-con="'+item.content+'"  class="edit layui-btn"><a>编辑</a></span>'
			                +'<span data-id="'+item.qid+'" class="delete layui-btn layui-btn-danger"><a>删除<a></span></td></tr>';
   					});
   					$('table tbody').append(html);
   					var form = layui.form();
   					form.render();
   				}
			});	
		},

		bindClickEvent: function bindClickEvent() {
			layer = layui.layer,form = layui.form();
			$('.delete').on("click",function(){
				var obj = $(this);
				layer.confirm('确定删除?', function(index){
						$.post("question/del/"+obj.data("id"),{},function(result){
							if (result.isOk) {
								obj.parent().parent().remove();
							}
						});
				   layer.close(index);
				});   
			});
			$(".edit").on("click",function(){
				var obj = $(this);
				layer.confirm('确定修改?', function(index){
					/* $.post("question",{'id':obj.attr("data-id")},function(result){
						if (result.isOk) {
							window.location.href="edit?id="+obj.attr("data-id");
						}
					}); */
					 layer.close(index);
					 layer.prompt({
					   formType: 2,
					   value: obj.data('con'),
					   title: '修改题干',
					   area: ['400px', '250px'] //自定义文本域宽高
					 }, function(value, index, elem){
						 	var data = 'question.qid='+obj.data('id')+'&question.content='+value;
						   	$.ajax({
								type:"POST",
					            url:"question/edit",
					            data: data,
					            dataType : 'json',
					           	success: function(result, status, xhr) {
					   				if(result.isOk){
					   					layer.close(index);
					   					layer.msg(result.msg, function(){
					   						window.location.reload();
					   					});
					   					}
					   				}
					        });
					 });
				}); 
			});

			$(".add").on("click",function(){
				var obj = $(this);
					 layer.prompt({
					   formType: 2,
					   value: obj.data('con'),
					   title: '新增试题',
					   area: ['400px', '250px'] //自定义文本域宽高
					 }, function(value, index, elem){
						 	var data = '&question.content='+value;
						   	$.ajax({
								type:"POST",
					            url:"question/add",
					            data: data,
					            dataType : 'json',
					           	success: function(result, status, xhr) {
					   				if(result.isOk){
					   					layer.close(index);
					   					layer.msg(result.msg, function(){
					   						window.location.reload();
					   					});
					   					}
					   				}
					        });
					 });
			
			});

			form.on('switch',function(data){
				var _this = $(this);
				var show = _this.attr('data-show') == 0 ? 1 : 0;
				$.ajax({type : "POST",dataType: 'json',async:false,
					    url : 'question/_updIsShow/'+_this.data('id'),
					    data : {isshow:show},
					    success: function(result, status, xhr) {
			   			  _this.attr('data-show', result.obj.isShow);
			   			}
				});
			   			
				
			});
			
			form.on('checkbox(allselector)', function(data) {
				var elem = data.elem;
				$('tbody').children('tr').each(function() {
					var $that = $(this);
					//全选或反选
					$that.children('td').eq(0).children('input[type=checkbox]')[0].checked = elem.checked;
					form.render('checkbox');
				});
			});

			$('.godown').on('click', function(){

				window.location.href = 'timing';
			});
		}
	};	
</script>
</head>

<body class="layui-bg-gray">

	<div class="layui-main layui-field-box layui-form">
		<h1 align="center">我是标题</h1>
		<div class="layui-btn-group">
			<button class="godown layui-btn  layui-btn-normal" title="返回" >
				<i class="layui-icon"> &#xe603;</i>
			</button>
			<button class="add layui-btn  layui-btn-normal" title="新增" >
				<i class="layui-icon"> &#xe654;</i>
			</button>
		</div>
		<table class="layui-table admin-table">
			<thead>
				<tr>
					<th style="width: 30px;"><input type="checkbox"
						lay-filter="allselector" lay-skin="primary" id="getSelected"></th>
					<th>序号</th>
					<th style="width: 50%;">题干</th>
					<th>答题时间</th>
					<th>是否显示</th>
					<th>介绍时间</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
	</div>
</body>
</html>
